<?php


namespace app\common\model;


use think\Model;
use traits\model\SoftDelete;

/**
 * 订单类
 * Class Order
 * @package app\common\model
 */
class Order2 extends Model
{
    use SoftDelete;


    // 表名
    protected $name = 'play_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = 'delete_dt';
    protected $connection = 'slavedatabase';

    // 追加属性
    protected $append = [
        "place_time_text",
        "expire_time_text",
        'order_status_text',
        'proof',
        'is_pay_text'
    ];



    public function user()
    {
        return $this->belongsTo('User2', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function shop()
    {
        return $this->belongsTo('app\admin\model\Shop2', 'shop_no', 'shop_no');
    }

    public function play()
    {
        return $this->belongsTo('app\admin\model\Play2', 'play_no', 'play_no');
    }

    public function play2()
    {
        return $this->belongsTo('app\admin\model\Play2', 'play_no', 'play_no', [], 'LEFT')->setEagerlyType(0);
    }

    // public function proof()
    // {
    //     return $this->hasMany('app\common\model\OrderProof', 'play_order_no', 'play_order_no');
    // }

    public function getOrderStatusList()
    {
        return ['1' => '待上传', '2' => '待审核', '3' => '已完成',  '6' => '待商家复审', '-1' => '用户取消','-2'=>'超时取消','-3'=>'已驳回','-5'=>'不满意反馈','-6'=>'不满意通过','-7'=>'异常订单','-8'=>'异常订单（客服已处理）','-9'=>'异常订单（已完成）','-4' => '异常订单（已取消）'];
        // return ['1' => '待上传', '2' => '待审核', '3' => '已完成', '-1' => '已取消','-2'=>'已取消','-3'=>'已驳回','-4' => '已取消','-5'=>'不满意反馈','-6'=>'不满意通过','-7'=>'处理中'];
    }


    public function getIsPayList()
    {
        return ['1' => '已结算', '0'=>'未结算'];
    }

    public function getOrderStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
        $list = $this->getOrderStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsPayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_pay']) ? $data['is_pay'] : '');
        $list = $this->getIsPayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getPlaceTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['place_time']) ? $data['place_time'] : '');
        return is_numeric($value) ? date("Y/m/d H:i", $value) : $value;
    }


    // public function getProofAttr($value, $data)
    // {
    //     return [];
    // }


    public function getexpireTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expire_time']) ? $data['expire_time'] : '');
        return is_numeric($value) ? date("Y/m/d H:i", $value) : $value;
    }
}